package cn.wolfcode.crmspringboot.web.controller;


import cn.wolfcode.crmspringboot.domain.UserInfo;
import cn.wolfcode.crmspringboot.redis.RedisCore;
import cn.wolfcode.crmspringboot.service.IEmployeeService;
import cn.wolfcode.crmspringboot.service.IPermissionService;
import cn.wolfcode.crmspringboot.service.impl.UserInfoService;
import cn.wolfcode.crmspringboot.util.JsonResult;
import cn.wolfcode.crmspringboot.util.UserContext;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private UserInfoService userInfoService;




//    @RequestMapping("/loginUser")
//    @ResponseBody
//    public JsonResult login(String username, String password){
//        try {
//            Employee employee = employeeService.login(username, password);
//            //把登录用户存到session中
//            UserContext.setCurrentUser(employee);
//            //如果不是超管,就要把用户的权限数据存到session中
//            if(!employee.isAdmin()){
//                List<String> permissions = permissionService.selectByEmpId(employee.getId());
//                UserContext.setPermissions(permissions);
//            }
//            return new JsonResult();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new JsonResult(false,"账号或密码错误");
//        }
//    }

    @RequestMapping("/loginUser")
    @ResponseBody
    public JsonResult login(String phone, String password){
        UserInfo user = userInfoService.getUserInfo(phone);
        if(user == null) return new JsonResult(false,"账号不保存在请重新输入");
        if (user.getStatus().equals("1")) return new JsonResult(false,"账号已经被管理员禁用,请联系管理员解封后使用");
        Md5Hash md5Hash = new Md5Hash(password,phone);
        if(!user.getPassword().equals(md5Hash.toString())) return new JsonResult(false,"密码不正确请重新输入");
        UserContext.setCurrentUserInfo(user);
        userInfoService.updateUserState(user.getPhone(),"在线");
        return new JsonResult();
    }

    @RequestMapping("/registerZ")
    @ResponseBody
    public JsonResult register(UserInfo user){
        if(user == null){
            return new JsonResult(false,"没有填写数据,请重新填写数据");
        }
        check(user);
        UserInfo userInfo = userInfoService.getUserInfo(user.getPhone());
        if(userInfo != null){
            return new JsonResult(false,"该账号已经创建,请直接登录");
        }
        String userId = UUID.randomUUID().toString().replaceAll("-", "");
        user.setUserId(userId);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),user.getPhone());
        user.setPassword(md5Hash.toString());
        userInfoService.saveUser(user);
        return new JsonResult(true,"注册成功赶快去登录吧");
    }


    @RequestMapping("/logout")
    public String logout(HttpSession session){
        UserInfo currentUser = UserContext.getCurrentUser();
        userInfoService.updateUserState(currentUser.getPhone(),"离线");
        session.invalidate();
        return "/login.html";
    }

    private void check(UserInfo userInfo){
        if(StringUtils.isEmpty(userInfo.getName())){
            throw new RuntimeException("姓名不能为空");
        }
        if(StringUtils.isEmpty(userInfo.getPhone())){
            throw new RuntimeException("手机号不能为空");
        }
        if(StringUtils.isEmpty(userInfo.getPassword())){
            throw new RuntimeException("密码不能为空");
        }
        if(StringUtils.isEmpty(userInfo.getAge())){
            throw new RuntimeException("年龄不能为空");
        }



    }





}
